<?php
    $_POST['error'] = array ('date'=>FALSE, 'name'=>FALSE, 'cost'=>FALSE);

    include('normalizeDate.php');
    include('normalizeName.php');
    include('normalizeCost.php');
    include('validateDate.php');
    include('validateName.php');
    include('validateCost.php');
    
    if($_POST['error']['date'] == FALSE and $_POST['error']['name'] == FALSE and $_POST['error']['cost'] == FALSE)
    {
        $expenseToAdd = $date.'@'.$name.'@'.$cost.'@'.$_POST['type']."\n";
        file_put_contents('listOfExpenses.txt', $expenseToAdd, FILE_APPEND);
        echo 'The expense was successfully added to the list.'.'<br>';
        echo '<a href="expense.php">Add a new expense</a><br>';
        echo '<a href="index.php">Go to the list of expenses</a>';
    }
    else
    {
        $errorMessage = '';
        if($_POST['error']['date'] == TRUE)
        {
            $errorMessage = $errorMessage.'The date was not in acceptable format. ';
        }
        if($_POST['error']['name'] == TRUE)
        {
            $errorMessage = $errorMessage.'The name must be over 3 and under 26 symbols ';
        }
        if($_POST['error']['cost'] == TRUE)
        {
            $errorMessage = $errorMessage.'The cost was not in acceptable format. ';
        }

        echo $errorMessage.'<br>';
        echo '<a href="expense.php">Add a new expense</a><br>';
        echo '<a href="index.php">Go to the list of expenses</a>';
    }
?>
